package com.GroupSix.controller;

import com.GroupSix.entity.ExamPaperEntity;
import com.GroupSix.service.ExamPaperService;
import com.GroupSix.vo.ExamPaperVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("backend/examPaper")
public class ExamPaperController {
    //字段注入
    @Autowired
    private ExamPaperService examPaperService;

    //增
    @PostMapping("add")
    public String add(@RequestBody ExamPaperEntity examPaperEntity) {
        Integer i = examPaperService.add(examPaperEntity);
        if (i > 0) {
            return "已成功添加";
        } else {
            return "添加失败";
        }
    }

    //删
    @PostMapping("delete/{id}")
    public String delete(@PathVariable("id") int id) {
        Integer i = examPaperService.delete(id);
        if (i > 0) {
            return "已成功删除";
        } else {
            return "删除失败";
        }
    }

    //查
    @PostMapping("find/{id}")
    public ExamPaperEntity find(@PathVariable("id") int id) {
        return examPaperService.find(id);
    }

    //改
    @PostMapping("update")
    public String update(@RequestBody ExamPaperEntity examPaperEntity) {
        Integer i = examPaperService.update(examPaperEntity);
        if (i > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    //分页查询
    @PostMapping("list")
    public IPage<ExamPaperEntity> list(@RequestBody ExamPaperVo vo) {
        ExamPaperEntity examPaper = new ExamPaperEntity();
        examPaper.setId(vo.getId());
        return examPaperService.list(examPaper, vo.getCurPage(), vo.getPageSize());
    }

    //根据试题生成规则生成试卷
    @PostMapping("generateExamPaper")
    public ExamPaperEntity generateExamPaper(@RequestParam ("ruleId") Long ruleId, @RequestParam ("teacherId") Long teacherId,
                                             @RequestParam ("type") String type) {
        return examPaperService.generateExamPaper(ruleId, teacherId, type);
    }
}
